﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccess;

namespace XuaNhapKho
{
    public partial class NhomSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadGridView();
                loadData();
            }
        }

        private void loadData()
        {
            grvNhomSP.DataSource = NhomSanPhamDAO.GetAll();
            grvNhomSP.DataBind();
        }

        private void loadGridView()
        {
            if (Request.QueryString["id"] != null)
            {
                int id = int.Parse(Request.QueryString["id"].ToString());
                DataTable dt = NhomSanPhamDAO.GetBy(id).Tables[0];

                foreach (DataRow rows in dt.Rows)
                {
                    txtNhomSP.Text = rows["TenNhomSanPham"].ToString();
                    
                }

            }
            
        }

        protected void lbtnEdit_Command(object sender, CommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            Response.Redirect("/NhomSanPham.aspx?id=" + id);
        }

        protected void lbtnRemove_Command(object sender, CommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            NhomSanPhamDAO.Delete(id);
            Response.Redirect("/NhomSanPham.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string name = txtNhomSP.Text;
            
            if (Request.QueryString["id"] == null)
            {
                NhomSanPhamDAO.Insert(name);
                Response.Redirect("/NhomSanPham.aspx");
            }
            else
            {
                int id = int.Parse(Request.QueryString["id"].ToString());
                NhomSanPhamDAO.Update(id,name);
                Response.Redirect("/NhomSanPham.aspx");
            }
        }

        protected void grvNhomSP_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvNhomSP.PageIndex = e.NewPageIndex;
            loadGridView();
        }


    }
}